package it.canraid.leaflet.model;

public class StatusPill {
    private static final String ID_ASCII = "S";
    private static final int N_ELEMENTS = 1;
    public String desc = "Loren ipsum";

    public StatusPill(String desc) {
	this.desc = desc;
    }

    public String generateAscii() {
	String encodeDesc = desc.replace(";", "%1%");
	encodeDesc = encodeDesc.replace("|", "%2%");
	String response = "|" + ID_ASCII + ";" + encodeDesc + ";";
	return response;
    }

    public static StatusPill extractStatus(String ascii) {
	StatusPill response = new StatusPill("");
	if (ascii == null)
	    return response;

	String[] asciiDecode = ascii.split(";");
	for (int q = 0; q < asciiDecode.length; q++) {
	    if (!asciiDecode[q].equals("|" + ID_ASCII))
		continue;
	    if (q + N_ELEMENTS >= asciiDecode.length)
		break;
	    try {
		String desc_ = asciiDecode[++q];
		String decodeDesc = desc_.replace("%1%", ";");
		decodeDesc = decodeDesc.replace("%2%", "|");
		response.desc = decodeDesc;
		break;
	    } catch (NumberFormatException e) {
		e.printStackTrace();
		continue;
	    }
	}
	return response;
    }
}
